/*
 * Daniel R Padilla
 *
 * Copyright (c) 2008, Daniel R Padilla
 *
 * This copyrighted material is made available to anyone wishing to use, modify,
 * copy, or redistribute it subject to the terms and conditions of the GNU
 * Lesser General Public License, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this distribution; if not, write to:
 * Free Software Foundation, Inc.
 * 51 Franklin Street, Fifth Floor
 * Boston, MA  02110-1301  USA
 */
package org.databasefacade.update;


import org.databasefacade.select.Arithmetic;
import org.databasefacade.statements.CaseStatement;
import org.databasefacade.statements.SelectStatement;
import org.databasefacade.table.Table;
import org.databasefacade.table.column.Column;


public class Update
{
	public Table updateTable;
	public UpdateSetList assignments = new UpdateSetList();

	/**
	 * Aliasing of the update table does not
	 * seem to be ansiSQL92 standard. 
	 */
	public Update(Table table)
	{
		updateTable = table.getCopy();
	}
	
	public Table getUpdateTable()
	{
		return updateTable;
	}
	
	public UpdateSetList getAssignments()
	{
		return assignments;
	}
			
	public void set(Column target, Column source)
	{
		assignments.add(new UpdateSet(target, source));
	}
	
	public void set(Column target, Arithmetic calc)
	{
		assignments.add(new UpdateSet(target, calc));
	}
	
	public void set(Column target, SelectStatement selectStmt)
	{
		assignments.add(new UpdateSet(target, selectStmt));
	}
	
	public void set(Column target, CaseStatement caseStmt)
	{
		assignments.add(new UpdateSet(target, caseStmt));
	}
	
	public void set(Column target, Object value)
	{
		assignments.add(new UpdateSet(target, value));
	}
	
	public String getUpdateOnlySQL()
	{
		return "UPDATE " + getUpdateTable().getTableName();
	}
	
	public String getAssignmentOnlySQL(String ansiSubSelectFrom)
	{
		StringBuilder sb = new StringBuilder("");
		String comma = "\n   SET ";
		if ( ansiSubSelectFrom == null ) {
			ansiSubSelectFrom = "";
		}
		for ( int i = 0; i < assignments.size(); i++ ) {
			UpdateSet set = assignments.get(i);
			Column target = set.getSetTarget();
			sb.append(comma + getUpdateTable().getTableName() + "." + target.getColumnName() + " = ");
			if ( ansiSubSelectFrom.length() > 0 && set.getSetSourceType() == UpdateSet.COLUMN_SOURCE ) {
				sb.append("\n         (SELECT ");
				sb.append(set.getSourceSQL()); 
				sb.append("\n            " + ansiSubSelectFrom.replace("\n", "\n          ") + ")");
			} else {
				sb.append("(" + set.getSourceSQL() + ")"); 
			}
			comma = "\n      ,";
		}
		return sb.toString();
	}
	
	public String getSQL(String ansiSubSelectFrom)
	{
		return getUpdateOnlySQL() + getAssignmentOnlySQL(ansiSubSelectFrom);
	}
	
	public String toString() {
		return getSQL(null);
	}
}
